package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.PintuanLog;
import com.qianniu.napi.admin.entity.PintuanOpen;
import com.qianniu.napi.admin.entity.Sharestat;
import com.qianniu.napi.admin.service.IPintuanLogService;
import com.qianniu.napi.admin.service.IPintuanOpenService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 开团记录
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-11
 */
@RestController
public class PintuanOpenController {

    private static final Logger logger = LoggerFactory.getLogger(PintuanOpenController.class);

    @Autowired
    private IPintuanOpenService pintuanOpenService;

    @Autowired
    private IPintuanLogService pintuanLogService;

    @RequestMapping("/api/pintuanopen/list")
    @ResponseBody
    public JsonDO list(@RequestParam(value = "status", required = false) String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       HttpServletRequest req) {
        JsonDO json = new JsonDO();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = pintuanOpenService.selectPage(page, new EntityWrapper<PintuanOpen>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        json.setData(page.getRecords());
        return json;
    }

    @RequestMapping("/api/pintuanopen/add")
    @ResponseBody
    public JsonDO pintuanopenAdd(@Valid PintuanOpen pintuanopen,
                                 BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        pintuanopen.setCreatetime(new Date());
        pintuanopen.insert();
        return json;
    }

    @RequestMapping("/api/pintuanopen/modify")
    @ResponseBody
    public JsonDO pintuanopenUpdate(HttpServletRequest req,
                                    PintuanOpen pintuanopen) {
        JsonDO json = new JsonDO();
        pintuanopen.setModifytime(new Date());
        pintuanopen.updateById();
        return json;
    }

    @RequestMapping("/api/pintuanopen/detail")
    @ResponseBody
    public JsonDO getPintuanOpenDetailById(@RequestParam("token") String token,
                                           @RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        PintuanOpen pintuanopen = pintuanOpenService.selectById(id);
        json.setData(pintuanopen);
        return json;
    }

    @RequestMapping("/api/pintuan/groupuser/list")
    @ResponseBody
    public JsonDO groupuserList(@RequestParam("id") long id,
                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = pintuanLogService.selectPage(page, new EntityWrapper<PintuanLog>()
                .eq("ptoid", id));
        json.setData(page.getRecords());
        return json;
    }

    @RequestMapping("/api/winner/detail")
    @ResponseBody
    public JsonDO winnerList(@RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        PintuanOpen pintuanopen = pintuanOpenService.selectById(id);
        json.setData(pintuanopen);
        return json;
    }

    @RequestMapping("/api/order/share/stat")
    @ResponseBody
    public JsonDO orderShare(Sharestat sharestat, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        sharestat.setUid(ObjectUtil.getUid());
        sharestat.setMid(ObjectUtil.getMid());
        sharestat.insert();
        return json;
    }

}
